<template>
    <div class="upload-container">
        <el-upload
            :action="remakeUrl('/upload/uploadFile')"
            accept=".pdf"
            :show-file-list="false"
            :on-success="uploadSuccess"
            :on-error="uploadError"
            :before-upload="uploadBefore"
            :disabled="disabled"
        >
            <div class="layout-block">
                <div v-loading="uploading" class="layout-item upload-btn" :class="{'has-img':singleHasImg}">
                    <i :class="uploadedImgs?'el-icon-edit-outline':'el-icon-plus'" />
                    <i v-show="uploadedImgs" class="el-icon-view upload-view-icon" @click.stop="imgView(uploadedImgs)" />
                    <div v-show="uploadedImgs" class="uploaded-single">
                        {{ fileName }}
                    </div>
                </div>
            </div>
        </el-upload>
    </div>
</template>

<script>
import { remakeUrl } from '@/utils'

export default {
    name: 'UploadImgList',
    props: {
        value: {
            type: String,
            default: ''
        },
        disabled: {
            type: Boolean,
            default: false
        }
    },
    data: function() {
        return {
            uploadedImgs: '',
            dialogVisible: false,
            dialogImageUrl: '',
            uploading: false
        }
    },
    computed: {
        singleHasImg() {
            return !this.multiple && this.uploadedImgs;
        },
        fileName() {
            const name = this.uploadedImgs.split('/').pop().split('_')
            name.pop()
            return name.join('') + '.pdf'
        }
    },
    watch: {
        value() {
            this.uploadedImgs = this.value;
        },
        uploadedImgs(val) {
            this.$emit('input', this.uploadedImgs);
        }
    },
    created() {
        this.uploadedImgs = this.value || '';
    },
    methods: {
        remakeUrl,
        uploadBefore(event, file, fileList) {
            if (this.uploading) return false;
            this.uploading = true;
        },
        uploadError(err) {
            console.log(err);
            this.uploading = false;
        },
        uploadSuccess(res) {
            this.uploading = false;
            this.uploadedImgs = res.data;
        },
        imgView(url) {
            window.open(url);
        }
    }
}
</script>

<style lang="scss" scoped>
.upload-container {
    .layout-block {
        position: relative;
        width: 100%;
        height: 0;
        padding-bottom: 100%;
    }

    .layout-item {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        border-radius: 4px;
        background-color: #999;
        overflow: hidden;

        .uploaded-img {
            display: block;
            width: 100%;
            height: 100%;
            object-fit: contain;
        }

        .uploaded-tools {
            display: flex;
            align-items: center;
            justify-content: center;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            font-size: 20px;
            color: #e0e0e0;
            background-color: rgba(0, 0, 0, .5);
            opacity: 0;
            visibility: hidden;
            transition: .3s;

            i {
                padding: 5px;
                cursor: pointer;
                transition: .3s;

                &:hover {
                    color: #fff;
                }
            }

            .tools-move {
                position: absolute;
                bottom: 0;
                left: 0;
            }
        }

        &:hover {
            .uploaded-tools {
                opacity: 1;
                visibility: visible;
            }
        }
    }

    .upload-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px dashed #d9d9d9;
        background-color: #fbfdff;
        border-radius: 4px;
        transition: .3s;

        i {
            position: relative;
            font-size: 28px;
            color: #8c939d;
            transition: .3s;
            z-index: 5;
        }

        .uploaded-single{
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            line-height: 1.1;
            text-align: center;
            object-fit: contain;
            z-index: 1;
            transition: .3s;
        }
        &:hover {
            background-color: #e6ebf1;
            border-color: #1d38FF;
            i{
                color: #1d38FF;
            }
        }
        &.has-img {
            i {
                color: #fff;
                opacity: 0;
                transition: .3s;
                &:hover{
                    color: #1D38FF;
                }
            }
            .upload-view-icon{
                margin-left: 4px;
                cursor: pointer;
            }
            &:hover{
                i {
                    opacity: 1;
                    filter: drop-shadow(0px 0px 3px #aaa);
                }
            }
        }
    }

    ::v-deep {
        .el-upload {
            display: block;
        }
    }
}
</style>
